package com.vincent.template.service.logic.impl;

import com.google.common.collect.Lists;
import com.vincent.template.commons.exception.Vct520Exception;
import com.vincent.template.domain.SysAuthGroupPo;
import com.vincent.template.dto.sys.authgroup.AuthGroupDto;
import com.vincent.template.repository.SysAuthGroupRepository;
import com.vincent.template.repository.SysBindAuthGroupAuthRepository;
import com.vincent.template.repository.SysBindAuthGroupRoleRepository;
import com.vincent.template.service.logic.AuthGroupService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Vincent Wang
 * @date 2021/3/25 18:32
 */
@Service
public class AuthGroupServiceImpl implements AuthGroupService {

    @Autowired
    private SysAuthGroupRepository authGroupRepository;
    @Autowired
    private SysBindAuthGroupAuthRepository bindAuthGroupAuthRepository;
    @Autowired
    private SysBindAuthGroupRoleRepository bindAuthGroupRoleRepository;

    @Override
    public List<AuthGroupDto> findAuthGroups() {
        List<SysAuthGroupPo> authGroups = authGroupRepository.findAll();
        if (CollectionUtils.isEmpty(authGroups)) {
            return Lists.newArrayList();
        }
        return authGroups.stream().map(group -> {
            AuthGroupDto groupDto = new AuthGroupDto();
            BeanUtils.copyProperties(group, groupDto);
            return groupDto;
        }).collect(Collectors.toList());
    }

    @Override
    public AuthGroupDto findAuthGroupById(Long id) {
        SysAuthGroupPo group = authGroupRepository.findById(id).orElse(null);
        if (group == null) {
            return null;
        }
        AuthGroupDto groupDto = new AuthGroupDto();
        BeanUtils.copyProperties(group, groupDto);
        return groupDto;
    }

    @Override
    public Long addAuthGroup(AuthGroupDto authGroupDto) {
        SysAuthGroupPo group = authGroupRepository.findByCode(authGroupDto.getCode());
        Vct520Exception.throwIfTrue(group != null, "权限组Code已存在：" + authGroupDto.getCode());
        group = new SysAuthGroupPo();
        BeanUtils.copyProperties(authGroupDto, group);
        return authGroupRepository.save(group).getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateAuthGroup(Long id, AuthGroupDto authGroupDto) {
        SysAuthGroupPo group = authGroupRepository.findById(id).orElse(null);
        Vct520Exception.throwIfEmpty(group, "权限组不存在");
        if (!group.getCode().equals(authGroupDto.getCode())) {
            Vct520Exception.throwIfTrue(authGroupRepository.findByCode(authGroupDto.getCode()) != null,
                    "权限组Code已存在：" + authGroupDto.getCode());
        }
        BeanUtils.copyProperties(authGroupDto, group, "id");
        authGroupRepository.save(group);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteAuthGroup(Long id) {
        SysAuthGroupPo group = authGroupRepository.findById(id).orElse(null);
        Vct520Exception.throwIfEmpty(group, "权限组不存在");
        Vct520Exception.throwIfTrue(CollectionUtils.isNotEmpty(bindAuthGroupAuthRepository.findByAuthGroupId(id)),
                "有权限绑定了该权限组，请先确认并解绑");
        Vct520Exception.throwIfTrue(CollectionUtils.isNotEmpty(bindAuthGroupRoleRepository.findByAuthGroupId(id)),
                "有角色绑定了该权限组，请先确认并解绑");
        authGroupRepository.delete(group);
        return true;
    }
}
